/*
 * Project: Sudoku Solver
 * File: cell.c
 *
 * Copyright (C) 2009 Daniel Meekins
 * Contact: dmeekins - gmail
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include "cell.h"

cell_t *cell_new(uint32_t num)
{
    cell_t *c;
    
    if(num > CELL_MAX_NUM)
        return NULL;
    
    if((c = (cell_t *)malloc(sizeof(cell_t))) == NULL)
        return NULL;
    
    cell_set_num(c, num);
    
    return c;
}

void cell_set_num(cell_t *c, uint32_t num)
{
    if(num <= CELL_MAX_NUM && num > 0)
        c->num = 0x1 << (num - 1);
    else
        c->num = ~0;
}

void cell_set(cell_t *c)
{
    c->num = ~0;
}

void cell_clear(cell_t *c)
{
    c->num = 0;
}

uint32_t cell_get_num(cell_t *c)
{
    uint32_t num;
    int i;
    
    if(!cell_issolved(c))
        return 0;
    
    num = 0;
    
    for(i = 0; i < CELL_MAX_NUM; i++)
    {
        if(((c->num >> i) & 0x1) == 1)
        {
            num = i + 1;
            break;
        }
    }
    
    return num;
}

void cell_fill_possible_num_count(cell_t* c, uint32_t num_count[CELL_MAX_NUM])
{
    int i;
    
    for(i = 0; i < CELL_MAX_NUM; i++)
    {
        if(((c->num >> i) & 0x1) == 1)
            num_count[i]++;
    }
}

uint32_t cell_possible_count(cell_t *c)
{
    uint32_t count;
    int i;
    
    count = 0;
    
    for(i = 1; i <= CELL_MAX_NUM; i++)
        if(cell_num_ispossible(c, i))
            count++;
    
    return count;
}

void cell_add_possible_nums(cell_t *dst, cell_t *poss)
{
    cell_union(dst, dst, poss);
}

void cell_remove_possible_nums(cell_t *dst, cell_t *poss)
{
    cell_t tmp;
    
    cell_set_num(&tmp, 0);
    
    if(!cell_issolved(dst))
    {
        cell_diff(&tmp, dst, poss);
        cell_intersect(dst, dst, &tmp);
    }
}

void cell_intersect(cell_t *c, cell_t *c1, cell_t *c2)
{
    uint32_t n1, n2;
    
    n1 = c1->num;
    n2 = c2->num;
    
    c->num = n1 & n2;
}

void cell_union(cell_t *c, cell_t *c1, cell_t *c2)
{
    uint32_t n1, n2;
    
    n1 = c1->num;
    n2 = c2->num;
    
    c->num = n1 | n2;
}

void cell_diff(cell_t *c, cell_t *c1, cell_t *c2)
{
    uint32_t n1, n2;
    
    n1 = c1->num;
    n2 = c2->num;
    
    c-> num = n1 ^ n2;
}

void cell_invert(cell_t *c)
{
    c->num = ~c->num;
}

void cell_copy(cell_t *dst, cell_t *src)
{
    dst->num = src->num;
}

int cell_num_ispossible(cell_t *c, uint32_t num)
{
    if(num > CELL_MAX_NUM || num <= 0)
        return 0;
    
    return ((c->num >> (num - 1)) & 0x1) == 1;
}

int cell_issolved(cell_t *c)
{
    int count, i;
    
    count = 0;
    
    for(i = 0; i < CELL_MAX_NUM; i++)
    {
        if(((0x1 << i) & c->num) != 0)
            count++;
    }
    
    return (count == 1) ? 1 : 0;
}

int cell_isequal(cell_t *c1, cell_t *c2)
{
    return c1->num == c2->num;
}

void cell_free(cell_t *c)
{
    free(c);
}

char numstring[CELL_MAX_NUM];

char *cell_num_string(cell_t *c)
{
    int i;
    
    for(i = 0; i < CELL_MAX_NUM; i++)
        numstring[CELL_MAX_NUM-i-1] = cell_num_ispossible(c, i+1) ? '1' : '0';
    
    return numstring;
}
